package com.jt.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @ClassName JedisDatasource
 * @Description TODO
 * @Author Jiyao
 * @DATE 2021-12-03 17:24
 * @Version 1.0
 **/
public class JedisDatasource {

    private static final String IP = "192.168.236.128";
    //redis.conf默认端口 6379
    private static final int PORT = 6379;
    private static JedisPool jedisPool;
    //static {
    //    JedisPoolConfig config = new JedisPoolConfig();
    //    //最大连接数
    //    config.setMaxTotal(16);
    //    //最大空闲连接数
    //    config.setMaxIdle(8);
    //    jedisPool = new JedisPool(config,IP, PORT);
    //}
    //
    //public static Jedis getConnection(){
    //    return jedisPool.getResource();
    //}

    public static Jedis getConnection(){
        if (jedisPool == null){
            synchronized (JedisDatasource.class){
                if (jedisPool == null){
                    JedisPoolConfig config = new JedisPoolConfig();
                    config.setMaxTotal(16);
                    config.setMaxIdle(8);
                    jedisPool = new JedisPool(config,IP, PORT);
                }
            }
        }
        return jedisPool.getResource();
    }

    public static void close(){
        jedisPool.close();
    }
}
